﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace SherlockGUI.CFG
{
    //入库时，库位选择方式。
   

    class SqlCfg
    {
        public string strServer = "Localhost";
        public string strDatabase = "db_wms";
        public string strUID = "root";
        public string strPassWord = "";
    }

    class SysCfg
    {
        //程序启动后，Sherlock 的状态
        public bool bAutoRun = false;
    }

    class SherlockCfg
    {
        public string strProgramPath;
        public string strImgWindowsName;
        public string strDrivers;
        public double dPicZoom;
    }

    class PicLogCfgBase
    {
        //注意：这个路径，
        public string strPicPath;
        public bool bSavePic;
        public int iMaxPicNum;
    }

    class InspectCountCfg
    {
        //是否保存检测结果。
        public bool bSaveCount;

        public int iOKCount = 0;

        public int iNGCount = 0;
    }

    //
    class LogCfg
    {
        public string strPicLogPath;

        public PicLogCfgBase PicLogCfg_OK = new PicLogCfgBase();
        //

        public PicLogCfgBase PicLogCfg_NG = new PicLogCfgBase();

        public InspectCountCfg InspectCount = new InspectCountCfg();
    }

    class OperatorCfg
    {
        public string strCurUserName = "";
        //用户是否已经登陆
        public bool bLogIn = false;
    }

    class UserValues
    {
        public GUI m_GUI = null;
    }

    class GlobalCfg
    {
        public static SqlCfg m_SqlCfg = new SqlCfg();

        public static SysCfg m_SysCfg = new SysCfg();

        public static SherlockCfg m_SherlockCfg = new SherlockCfg();

        public static LogCfg m_LogCfg = new LogCfg();

        //
        public static UserValues m_UserValues = new UserValues();

        public static OperatorCfg m_OperatorCfg = new OperatorCfg();

        //////////////////////////////////////////////////////////////////////////
        private static IniFile m_iniFile = new IniFile(Application.StartupPath + "\\HzHeHuaVision.ini");

        public static void LoadCfg()
        {
            //////////////////////////////////////////////////////////////////////////
            m_SqlCfg.strServer = m_iniFile.readIni("Sql", "Server");
            if (m_SqlCfg.strServer.Length < 5)
            {
                m_SqlCfg.strServer = "Localhost";
            }
            m_SqlCfg.strUID = m_iniFile.readIni("Sql", "UID");
            if (m_SqlCfg.strUID.Length < 1)
            {
                m_SqlCfg.strUID = "root";
            }
            m_SqlCfg.strPassWord = m_iniFile.readIni("Sql", "PassWord");
            if (m_SqlCfg.strPassWord.Length < 1)
            {
                m_SqlCfg.strPassWord = "";
            }

          
            //////////////////////////////////////////////////////////////////////////
            m_SysCfg.bAutoRun = m_iniFile.ReadBool("Sys", "AutoRun", true);


            //////////////////////////////////////////////////////////////////////////
            m_SherlockCfg.strProgramPath = m_iniFile.readString("SherlockProgram", "SherlockPath", "");
            m_SherlockCfg.strImgWindowsName = m_iniFile.readString("SherlockProgram", "strImgWindowsName", "imgA");
            m_SherlockCfg.strDrivers = m_iniFile.readString("SherlockProgram", "Drivers","");
            m_SherlockCfg.dPicZoom = m_iniFile.readDouble("SherlockProgram", "PicZoom",-1);
            if (m_SherlockCfg.dPicZoom < 0.00001)
            {
                m_SherlockCfg.dPicZoom = -1;
            }

            /////////////////////////////////////////////////////////////////////////////
            m_LogCfg.strPicLogPath = m_iniFile.readString("Log", "PicPath", "");

            m_LogCfg.PicLogCfg_OK.bSavePic = m_iniFile.ReadBool("Log", "SavePic_OK", false);
            m_LogCfg.PicLogCfg_OK.iMaxPicNum = m_iniFile.ReadInt("Log", "SavePicNum_Ok", 1000);
            if (m_LogCfg.PicLogCfg_OK.bSavePic == true)
            {
                m_LogCfg.PicLogCfg_OK.strPicPath = m_LogCfg.strPicLogPath.TrimEnd('\\');
                m_LogCfg.PicLogCfg_OK.strPicPath += "\\OK\\";
                if (!System.IO.Directory.Exists(m_LogCfg.PicLogCfg_OK.strPicPath))
                {
                    System.IO.Directory.CreateDirectory(m_LogCfg.PicLogCfg_OK.strPicPath);//不存在就创建目录   
                }
            }
            m_LogCfg.PicLogCfg_NG.bSavePic = m_iniFile.ReadBool("Log", "SavePic_NG", false);
            m_LogCfg.PicLogCfg_NG.iMaxPicNum = m_iniFile.ReadInt("Log", "SavePicNum_NG", 1000);
            if (m_LogCfg.PicLogCfg_NG.bSavePic == true)
            {
                m_LogCfg.PicLogCfg_NG.strPicPath = m_LogCfg.strPicLogPath.TrimEnd('\\');
                m_LogCfg.PicLogCfg_NG.strPicPath += "\\NG\\";
                if (!System.IO.Directory.Exists(m_LogCfg.PicLogCfg_NG.strPicPath))
                {
                    System.IO.Directory.CreateDirectory(m_LogCfg.PicLogCfg_NG.strPicPath);//不存在就创建目录   
                }
            }

            /////////////////////////////////////////////////////////////////////////////////
            m_LogCfg.InspectCount.bSaveCount = m_iniFile.ReadBool("Result", "SaveInspectCount", true);

            if (m_LogCfg.InspectCount.bSaveCount == true)
            {
                m_LogCfg.InspectCount.iOKCount = m_iniFile.ReadInt("Result", "OKCount", 0);
                m_LogCfg.InspectCount.iNGCount = m_iniFile.ReadInt("Result", "NGCount", 0);
            }
        }

        public static bool CheckUserPWD(string strUserName, string strPWD)
        {
            bool bRet = false;

            string strRet = m_iniFile.readIni("LogIn", strUserName);
            if (strRet == strPWD)
            {
                bRet = true;
            }
            else
            {
                bRet = false;
            }

            return bRet;
        }

        public static void SaveSherlockCfg()
        {
            m_iniFile.WriteBool("Sys", "AutoRun", m_SysCfg.bAutoRun);

            m_iniFile.WriteDouble("SherlockProgram", "PicZoom", m_SherlockCfg.dPicZoom);

            m_iniFile.writeIni("SherlockProgram", "SherlockPath", m_SherlockCfg.strProgramPath);
        }

        public static void SaveLogCfg()
        {
            m_iniFile.writeIni("Log", "PicPath", m_LogCfg.strPicLogPath);

            m_iniFile.WriteBool("Log", "SavePic_NG", m_LogCfg.PicLogCfg_NG.bSavePic);
            m_iniFile.WriteInt("Log", "SavePicNum_NG", m_LogCfg.PicLogCfg_NG.iMaxPicNum);
            m_iniFile.WriteBool("Log", "SavePic_OK", m_LogCfg.PicLogCfg_OK.bSavePic);
            m_iniFile.WriteInt("Log", "SavePicNum_OK", m_LogCfg.PicLogCfg_OK.iMaxPicNum);


            m_iniFile.WriteBool("Result", "SaveInspectCount", m_LogCfg.InspectCount.bSaveCount);
        }

        public static void SaveResultCount(string strOkCount,string strNgCount)
        {
            m_iniFile.WriteString("Result", "OKCount", strOkCount);
            m_iniFile.WriteString("Result", "NGCount", strNgCount);
        }
    }
}
